import Vue from 'vue'
import VueRouter from 'vue-router' // 引入路由的插件
// 所有的用户一定会打开首页 --- 先引入后调用
import Home from '../views/home/index.vue'

Vue.use(VueRouter) // 使用路由的插件
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
// 定义路由的规则
const routes = [
  {
    path: '/',
    redirect: '/home' // 路由重定向
  },
  {
    path: '/home',
    name: 'home', // 命名路由
    component: Home
  },
  {
    path: '/kind',
    name: 'kind',
    // 路由的懒加载  --- 需要的时候再加载
    component: () => import('../views/kind/index.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/cart/index.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/user/index.vue')
  },
  {
    path: '/detail/:proid',
    name: 'detail',
    component: () => import('../views/detail/index.vue'),
    meta: { // 通过 v-if 隐藏页面的底部
      // https://router.vuejs.org/zh/guide/advanced/meta.html
      hidden: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register/index.vue'),
    meta: {
      hidden: true
    },
    children: [ // 嵌套路由
      {
        path: 'step1', // 不要加 /
        name: 'register1',
        component: () => import('../views/register/index1.vue'),
        meta: {
          hidden: true
        }
      },
      {
        path: 'step2',
        name: 'register2',
        component: () => import('../views/register/index2.vue'),
        meta: {
          hidden: true
        }
      },
      {
        path: 'step3',
        name: 'register3',
        component: () => import('../views/register/index3.vue'),
        meta: {
          hidden: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '*',
    component: () => import('../views/error/notFound.vue')
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history', // 路由的模式  hash -- 带有#   history
  base: process.env.BASE_URL,
  routes
})

export default router
